using System;
using System.Collections;
using BitMango;
using Data;
using TMPro;
using UnityEngine;

public class GiftInfo : MonoBehaviour
{
	private void OnEnable()
	{
		Wallet.itemChangedCallback += this.OnChangedItem;
		base.StartCoroutine(this.CoGiftSystem());
	}

	private void OnDisable()
	{
		Wallet.itemChangedCallback -= this.OnChangedItem;
		base.StopCoroutine(this.CoGiftSystem());
	}

	private IEnumerator CoGiftSystem()
	{
		while (!Wallet.IsValid())
		{
			yield return new WaitForEndOfFrame();
		}
		for (;;)
		{
			if (GiftSystem.IsGift())
			{
				if (this.giftTime != null)
				{
					this.giftTime.gameObject.SetActive(false);
				}
				this.giftOn.SetActive(true);
				if (this.giftOff != null)
				{
					this.giftOff.SetActive(false);
				}
				this.text.SetActive(true);
			}
			else
			{
				if (this.giftTime != null)
				{
					this.giftTime.text = GiftSystem.GetGiftTime();
				}
				if (this.giftTime != null)
				{
					this.giftTime.gameObject.SetActive(true);
				}
				this.giftOn.SetActive(false);
				if (this.giftOff != null)
				{
					this.giftOff.SetActive(true);
				}
				this.text.SetActive(false);
			}
			yield return new WaitForSeconds(1f);
		}
		yield break;
	}

	private void OnChangedItem(InventoryItem item, int count)
	{
		if (item.id.Equals("freegift"))
		{
			base.StopCoroutine(this.CoGiftSystem());
			base.StartCoroutine(this.CoGiftSystem());
		}
	}

	private void OnClickGift()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		if (DataContainer<Data.Root>.Data.tutorialdata.record.isFirstFreeCoin)
		{
			DataContainer<Data.Root>.Data.tutorialdata.record.isFirstFreeCoin = false;
			DataContainer<Data.Root>.Data.tutorialdata.SaveContext();
			Scene.CurrentScene().gameObject.SendMessage("OnClickGift", this);
		}
		Scene.ShowPopup("FreeCoinRewardPopup", null, delegate(object isSuccess)
		{
			if ((bool)isSuccess)
			{
				SingletonMonoBehaviour<GiftSystem>.Instance.UseGift();
			}
		});
	}

	public GameObject giftOn;

	public GameObject giftOff;

	public TextMeshPro giftTime;

	public GameObject text;
}
